$( document ).ready(function() {
	$("#messageDiv").empty();
	$("#messageDiv").hide();
	$("[name='searchUserId']").val("");
	$("[name='searchFirstname']").val("");
	$("[name='searchLastname']").val("");
	$("[name='recordPatientId']").val("");
	/*
	$("[name='monthStart']").val("-1");
	$("[name='dayStart']").val("-1");
	$("[name='hourStart']").val("-1");
	$("[name='minuteStart']").val("-1");
	$("[name='monthEnd']").val("-1");
	$("[name='dayEnd']").val("-1");
	$("[name='hourEnd']").val("-1");
	$("[name='minuteEnd']").val("-1");
	*/
	$("#chooseStartDate").datepicker();
	$("#chooseStartTime").timepicker({'timeFormat': 'H:i'});
	$("#chooseEndDate").datepicker();
	$("#chooseEndTime").timepicker({'timeFormat': 'H:i'});
	$("[name='recordDiagnosis']").val("");
	$("[name='recordDrug']").val("");
	$("[name='recordInstruction']").val("");
	$("[name='recordDeaNum']").val("");
	$("[name='recordTreatSche']").val("");
	$("[name='recordComment']").val("");
	$("#chooseStartDate").val("");
	$("#chooseStartTime").val("");
	$("#chooseEndDate").val("");
	$("#chooseEndTime").val("");
});


function createRecord() {
	$.getJSON("doctor/createRecord.html?recordPatientId="+$("[name='recordPatientId']").val()+
			/*
			"&monthStart="+$("[name='monthStart']").val()+
			"&dayStart="+$("[name='dayStart']").val()+
			"&hourStart="+$("[name='hourStart']").val()+
			"&minuteStart="+$("[name='minuteStart']").val()+
			"&monthEnd="+$("[name='monthEnd']").val()+
			"&dayEnd="+$("[name='dayEnd']").val()+
			"&hourEnd="+$("[name='hourEnd']").val()+
			"&minuteEnd="+$("[name='minuteEnd']").val()+
			*/
			"&startDate="+$("#chooseStartDate").val()+
			"&startTime="+$("#chooseStartTime").val()+
			"&endDate="+$("#chooseEndDate").val()+
			"&endTime="+$("#chooseEndTime").val()+
			"&recordDiagnosis="+$("[name='recordDiagnosis']").val()+
			"&recordDrug="+$("[name='recordDrug']").val()+
			"&recordInstruction="+$("[name='recordInstruction']").val()+
			"&recordDeaNum="+$("[name='recordDeaNum']").val()+
			"&recordTreatSche="+$("[name='recordTreatSche']").val()+
			"&recordComment="+$("[name='recordComment']").val(), function(data){
		
		if(data.flag==true){
			$("#messageDiv").removeClass("success");
			$("#messageDiv").removeClass("error");
			$("#messageDiv").addClass("success");
			$("#messageDiv").html("<strong>"+data.message+"</strong>");
			$("#messageDiv").show();
			$("[name='searchUserId']").val("");
			$("[name='searchFirstname']").val("");
			$("[name='searchLastname']").val("");
			$("[name='recordPatientId']").val("");
			/*
			$("[name='monthStart']").val("-1");
			$("[name='dayStart']").val("-1");
			$("[name='hourStart']").val("-1");
			$("[name='minuteStart']").val("-1");
			$("[name='monthEnd']").val("-1");
			$("[name='dayEnd']").val("-1");
			$("[name='hourEnd']").val("-1");
			$("[name='minuteEnd']").val("-1");
			*/
			$("#chooseStartDate").val("");
			$("#chooseStartTime").val("");
			$("#chooseEndDate").val("");
			$("#chooseEndTime").val("");
			$("[name='recordDiagnosis']").val("");
			$("[name='recordDrug']").val("");
			$("[name='recordInstruction']").val("");
			$("[name='recordDeaNum']").val("");
			$("[name='recordTreatSche']").val("");
			$("[name='recordComment']").val("");
		}else if(data.flag==false){
			$("#messageDiv").removeClass("success");
			$("#messageDiv").removeClass("error");
			$("#messageDiv").addClass("error");
			$("#messageDiv").html("<strong>"+data.message+"</strong>");
			$("#messageDiv").show();
		}
	});
}

function getPatientSearchResult() {
	$("#patientResult").empty();
	$("#recordDetail").empty();
	$("#recordDetailView").empty();
	$("#recordDetailView").hide();
	$("#messageDiv").empty();
	$("#messageDiv").hide();
	$.getJSON("doctor/searchResult.html?searchUserId="+$("[name='searchUserId']").val()+"&searchFirstname="+$("[name='searchFirstname']").val()+"&searchLastname="+$("[name='searchLastname']").val(), function(data) {
		if(!jQuery.isEmptyObject(data.patientSearchResult)){
			$("#patientResult").data("patientSearchResult", data);
			printTableBody(data);
			$("#patientResult").append("<br /><button id=\"confirmPatient\" onclick=\"confirmPatient()\" class=\"button\">confirm</button>");
		}else{
			$("#patientResult").append("<strong>No result.</strong>");
		}
	});
}

function clearPtable(){
	$("#patientResult").empty();
}

function printTableBody(data) {
	
	$("#patientResult")
			.append(
					"<table id=\"list\" class=\"tablecss\">" +
					"<thead>" +
					"<tr>" +
					"<th></th>"+
					"<th>UserId</th>" +
					"<th>First name</th>" +
					"<th>Last name</th>" +
					"<th>Gender</th>" +
					"<th>Date of Birth</th>" +
					"</tr>" +
					"</thead>" +
					"<tbody id=\"tableBody\">" +
					"</tbody>" +
					"</table>");
	var tr;
	$(data).each(
			function(index, value) {
				$(value.patientSearchResult).each(function (i,v){
					tr = $("<tr>");
					tr.append("<td><input type=\"radio\" name=\"patientRadio\" value=\""+v.userId+"\"></td>");
					tr.append("<td>" + v.userId + "</td>");
					tr.append("<td>" + v.firstname + "</td>");
					tr.append("<td>" + v.lastname + "</td>");
					var gen=v.gender;
					if(gen=='m'){
						tr.append("<td>male</td>");
					}else if(gen=='f'){
						tr.append("<td>female</td>");
					}else{
						tr.append("<td></td>");
					}
					tr.append("<td>" + v.dateOfBirth.substring(0,10) +"</td>");
					tr.append("</tr>");
					$("#tableBody").append(tr);
				});
				
			});
	$("#list").tablesorter({
		widgets : [ 'zebra' ],
		headers : {
			0: {
				sorter : false
			}
		}
	});
}

function confirmPatient(){
	var id=$("[name=\"patientRadio\"]:checked").val();
	$("[name='recordPatientId']").val(id);
	$("#patientResult").empty();
	$("#messageDiv").empty();
	$("#messageDiv").hide();
}

function getAllPatients(){
	$("#patientResult").empty();
	$("#messageDiv").empty();
	$("#messageDiv").hide();
	$("[name='searchUserId']").val("");
	$("[name='searchFirstname']").val("");
	$("[name='searchLastname']").val("");
	$.getJSON("doctor/allPatients.html", function(data) {
		if(!jQuery.isEmptyObject(data.patientSearchResult)){
			$("#patientResult").data("patientSearchResult", data);
			printTableBody(data);
			$("#patientResult").append("<br /><button id=\"confirmPatient\" onclick=\"confirmPatient()\" class=\"button\">confirm</button>");
		}else{
			$("#patientResult").append("<strong>No result.</strong>");
		}
		
	});
}